package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.common.StatusComment;
import com.gxa.mapper.TransferOrderMapper;
import com.gxa.pojo.*;
import com.gxa.service.TransferOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class TransferOrderServiceImpl implements TransferOrderService {

    R r = new R();

    @Autowired(required = false)
    private TransferOrderMapper transferOrderMapper;

    @Override
    public R saveOrderForm(Receipt_Transfer receipt_transfer) {
        Goods_Save goods_save = null;
        Integer transfer_number = null;
        boolean flag = true;
        System.out.println(receipt_transfer.getList());
        for (Transfer transfer : receipt_transfer.getList()) {
            if(transfer.getOut_Warehouse_Id().equals(transfer.getIn_Warehouse_Id())){
                r.setCode(401);
                r.setMsg("仓库名不能相同");
            }else{
                goods_save = transferOrderMapper.queryGoodsNumber(transfer.getGoods_Id(),transfer.getOut_Warehouse_Id());
                transfer_number = transfer.getTransfer_Number();
                if(transfer_number > goods_save.getGoods_Save_Num()){
                    r.setCode(402);
                    r.setMsg("当前调用仓库库存不足，请重新选择");
                    flag = false;
                }
            }
        }
        if (flag){
            List<Transfer> transferList = receipt_transfer.getList();
            int result = transferOrderMapper.saveTransderOrder(transferList);
            int result2 = transferOrderMapper.saveReceiptTransfer(receipt_transfer);
            if(result > 0 && result2 > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }
        return r;
    }

    @Override
    public R queryTransferOrder(Map map) {
        int currentPage = Integer.parseInt((String) map.get("currentPage"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        Integer newCurrentPage = (currentPage - 1) * pageSize;
        List<Map> transferList =  transferOrderMapper.queryTransferOrder(newCurrentPage,pageSize,map,StatusComment.STATUS);
        if(transferList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(transferList);
            return r;
        }else {
            r.setCode(405);
            r.setMsg("系统出错了，请联系管理员");
            return r;
        }
    }

    @Override
    public R delTransferOrder(Map map) {
        int result = transferOrderMapper.delTransferOrder(map,StatusComment.DEL_STATUS);
        if(result > 0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
          r.setCode(500);
          r.setMsg("系统出错，请联系管理员");
        }
        return r;
    }

    @Override
    public R autitTransferOrder(Map map) {
        int result = transferOrderMapper.autitTransferOrder(map,StatusComment.AUTIT_STATUS);
        if(result > 0){
            String  in_warehouse_id = (String) map.get("in_warehouse_id");
            String goods_id = (String) map.get("goods_id");
            Goods_Save goods_save = transferOrderMapper.queryGoodsByWarehouseId(in_warehouse_id,goods_id);
            Transfer transfer = transferOrderMapper.queryTransferOrderGoodsNum(map);
            if(goods_save != null){
                transferOrderMapper.updateGoodsNumByIn2(transfer.getTransfer_Number(),map);
                transferOrderMapper.updateGoodsNumByOut2(transfer.getTransfer_Number(),map);
            }else {
                transferOrderMapper.updateGoodsNumByOut(transfer.getTransfer_Number(),map);
                transferOrderMapper.updateGoodsNumByIn(transfer.getTransfer_Number(),map);
            }
            r.setCode(200);
            r.setMsg("审核成功");
        }else {
            r.setCode(500);
            r.setMsg("系统出错，请联系管理员");
        }
        return r;
    }
}
